import logging

import pytest

from resource.pom.memorandum import Memorandum


class Test:
    app = Memorandum

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1：进入桌面，点击备忘录应用，点击收缩文件夹，点击“新建”按钮')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.click(*self.app.open_menu)
        device.refresh_layout()
        device.click_element(device.get_element_by_text('新建'))
        logging.info('预期结果1：弹出新建文件夹弹窗')
        device.refresh_layout()
        device.assert_text_exist('新建文件夹')

        logging.info('步骤2：输入名称，文件夹标签颜色选择')
        folder_name = device.get_element_by_condition(condition={'type': 'TextInput', 'hint': '名称'})
        device.click_element(folder_name)
        logging.info('预期结果2：点击名称可唤醒输入法正常输入，文件夹标签颜色可随意切换')
        device.refresh_layout()
        device.assert_soft_keyboard_on()

        color0_items = device.get_elements_by_type('Circle')
        text_input = device.get_element_by_condition(condition={'type': 'TextInput', 'hint': '名称'})
        cancel = device.get_element_by_text('取消')
        save = device.get_element_by_text('保存')
        device.click_element(color0_items[1])
        device.refresh_layout()
        color1_items = device.get_elements_by_type('Circle')
        # 选一个颜色后实际是会多一个circle覆盖在原来的颜色颜色选择上面
        assert len(color1_items) > len(color0_items)

        logging.info('步骤3：点击取消')
        device.click_element(cancel)
        logging.info('预期结果3：点击取消按钮，文件夹不会保存/点击保存按钮，笔记中不显示新建文件夹名称')
        device.refresh_layout()
        device.assert_text_exist('新建文件夹', expect=False)

        logging.info('步骤4：点击保存')
        device.click_element(device.get_element_by_text('新建'))
        device.click_element(folder_name)
        device.click_element(text_input)
        device.input_text(*device.center_of_element(text_input), text='abc')
        device.click_element(save)
        logging.info('预期结果4：点击保存按钮，笔记中显示新建文件夹名称')
        device.refresh_layout()
        device.assert_text_exist('abc')
